<?php

defined('ACC')||exit('Access Denied');

class UserModel extends Model{
	
	/* 添加用户注册信息 */
	public function reg($data){
		$data['passWord']=$this->encPass($data['passWord']);
		return $this->db->autoExecute($data,'user');
	}
	
	/* 给用户密码加密 */
	public function encPass($passWord){
		return md5($passWord);
	}
	
	/* 检查用户信息是否正确 */
	public function checkUser($username,$passWord=''){
		if($passWord==''){
			$sql="select count(*) from user where username='$username'";
			return $this->db->getOne($sql);
		}else{
			$passWord=$this->encPass($passWord);
			$sql="select user_id,username,email from user where username='$username' and password='$passWord'";
			return $this->db->getRow($sql);
		}	
	}
	
	/* 修改用户上次登陆时间 */
	public function upLog($user_id){
		$data=array('lastlogin'=>time());
		$this->db->autoExecute($data,'user','update',"user_id=$user_id");
	}
	
	/* 获取地址列表 */
	public function get_address_list($user_id){
		$sql = 'select * from user_address where user_id = ' . $user_id;
		$arr = $this->db->getAll($sql);
		if(is_array($arr)){
			foreach($arr as $key => $val){
				if(isset($val['country'])){
					$sql = 'select region_name from region where region_id = ' . $val['country'];
					$arr[$key]['country'] = $this->db->getOne($sql);
				}
				if(isset($val['province'])){
					$sql = 'select region_name from region where region_id = ' . $val['province'];
					$arr[$key]['province'] = $this->db->getOne($sql) . '省';
				}
				if(isset($val['city'])){
					$sql = 'select region_name from region where region_id = ' . $val['city'];
					$arr[$key]['city'] = $this->db->getOne($sql) . '市';
				}
				if(isset($val['district'])){
					$sql = 'select region_name from region where region_id = ' . $val['district'];
					$arr[$key]['district'] = $this->db->getOne($sql);
				}
			}
		}else{
			return false;
		}
		return $arr;
	}
	
	/* 添加一行收件人地址信息 */
	public function regionInsert($data, $table){
		return $this->db->autoExecute($data, $table);
	}
	
	/* 删除一行用户收件地址 */
	public function drop_consignee($address_id){
		$sql = 'delete from user_address where address_id = ' . $address_id;
		return $this->db->query($sql);
	}
	
	/* 获取用户的所有订单列表 */
	public function get_order_list($user_id){
		$sql = 'select order_id, order_sn, amount, pay_status, addtime from order_info where user_id = ' . $user_id;
		return $this->db->getAll($sql);
	}
	
	/* 获取用户信息 */
	public function get_user_info($user_id){
		$sql = 'select user_id, username, email, birthday, sex, tel, qq from user where user_id = ' . $user_id;
		return $this->db->getRow($sql);
	}
	
	/* 修改用户信息 */
	public function user_update($data, $where){
		return $this->db->autoExecute($data, 'user', 'update', $where);
	}
	
	/* 获取用户密码 */
	public function get_password($user_id){
		$sql = 'select password from user where user_id = ' . $user_id;
		return $this->db->getOne($sql);
	}
	
	/* 修改用户密码 */
	public function password_update($user_id, $new_password){
		$sql = 'update user set password = ' . 'md5(' . "'$new_password'" . ')' . ' where user_id = ' . $user_id;
		return $this->db->query($sql);
	}
	
	/* 获取某收件地址id下所有的地址信息 */
	public function get_user_address($address_id){
		$sql = 'select * from user_address where address_id = ' .$address_id;
		return $this->db->getRow($sql);
	}
	
	/* 修改收件地址信息 */
	public function addressUpdate($data, $table, $where){
		return $this->db->autoExecute($data, $table, 'update', $where);
	}
	
	/* 获取订单总条数 */
	public function get_order_count(){
		$sql = 'select count(*) from order_info';
		return $this->db->getOne($sql);
	}
	
	/* 删除一行订单 */
	public function delete_order($order_id){
		$sql = 'delete from order_info where order_id = ' . $order_id;
		return $this->db->query($sql);
	}
	
	/* 获取一条订单的信息 */
	public function get_order_info($order_id){
		$sql = 'select order_id, order_sn, amount, shipping_id, country, province, city, district, address, zipcode, tel, mobile, email, consignee from order_info where order_id = ' . $order_id;

		$order_info = $this->db->getRow($sql);
		foreach($order_info as $key => $val){
			if($key == 'country'){
				$region_val = $val;
				$region_key = $key;
			}else if($key == 'province'){
				$region_val = $val;
				$region_key = $key;
			}else if($key == 'city'){
				$region_val = $val;
				$region_key = $key;
			}else if($key == 'district'){
				$region_val = $val;
				$region_key = $key;
			}
			if(isset($region_key) && $region_key != ''){
				$sql = 'select region.region_name from region left join order_info on ' . $region_val . ' = region.region_id where order_id = ' . $order_id;
				$order_info[$region_key] = $this->db->getOne($sql);
			}
		}
		return $order_info;
	}
	
	/* 根据订单获取订单下的商品信息 */
	public function get_goods_list($order_id){
		$sql = 'select goods_id, goods_name, num, shop_price, subtotal from order_goods where order_id = ' . $order_id;
		return $this->db->getAll($sql);
	}
	
	/* 获取邮费价格 */
	public function get_shipping_price($shipping_id){
		$sql = 'select price from shipping where shipping_id = ' . $shipping_id;
		return $this->db->getOne($sql);
	}
	
	/* 获取上次登陆时间 */
	public function get_login_time($user_id){
		$sql = 'select lastLogin from user where user_id = ' . $user_id;
		return $this->db->getOne($sql);
	}

}




?>